<?php

class xineEventDispatcher {
	protected $listeners;
	
	public function __construct($listeners=array()) {
		$this->listeners = $listeners;
	}
	
	/*
	 * Callback string must be just pluginName
	 * @param $eventName
	 * @param $callback
	 */
	public function subscribeTo ($eventName, $callback) {
		$this->listeners[$eventName][] = $callback;
	}
	
	/**
	 * Notifies listeners about the event
	 * 
	 * @param $eventName
	 */
	public function notifyListeners ($eventName) {
		if(isset($this->listeners[$eventName])) {
			foreach($this->listeners[$eventName] as $listener) {
				call_user_func(array($listener, 'EVENT_'.$eventName));
			}
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * Alias for self::notifyListeners()
	 * 
	 * @param $eventName
	 */
	public function triggerEvent ($eventName) {
		return $this->notifyListeners($eventName);
	}
	
	/**
	 * Allows to register a custom event
	 * 
	 * @param $eventName
	 */
	public function registerCustomEvent ($eventName) {
		if(isset($this->listeners[$eventName])) {
			return false;
		} else {
			$this->listeners[$eventName] = array();
			return true;
		}
	}
}